package com.otaku.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otaku.admin.dao.RoleMapper;
import com.otaku.admin.dao.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("roleService")
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    public IPage<Role> findAll(Role role){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(role);
        IPage<Role> roleIPage = roleMapper.selectPage(role.getPage(), queryWrapper);
        return roleIPage;
    }
    public boolean addRole(Role role) {
        int insert = roleMapper.insert(role);
        return insert > 0;
    }

    public boolean deleteRole(Role role) {
        int i = roleMapper.deleteById(role.getId());
        return i > 0;
    }

    public boolean updateRole(Role role) {
        int i = roleMapper.updateById(role);
        return i > 0;
    }

    public List<Role> selectRoleByUser(String userName){
        return roleMapper.findByUser(userName);
    }

    public List<Role> selectRoleAll(){
        return roleMapper.findAll();
    }
}
